﻿// 
//=====================================================================================
// Repository: https://gitee.com/putzone/fofasearch
//=====================================================================================
//
namespace FOFA搜索
{
    class Registry
    {
        public static AccountConfig Load()
        {
            AccountConfig accountConfig = new AccountConfig() {
                Email = string.Empty,
                ApiKey = string.Empty,
                SearchKey = string.Empty,
                Fields = string.Empty,
                Size = string.Empty
            };
            Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\put.zone\FofaSearch");
            if (registryKey != null)
            {
                try
                {
                    accountConfig.Email = (string)registryKey.GetValue("Email");
                    accountConfig.ApiKey = (string)registryKey.GetValue("ApiKey");
                    accountConfig.SearchKey = (string)registryKey.GetValue("SearchKey");
                    accountConfig.Fields = (string)registryKey.GetValue("Fields");
                    accountConfig.Size = (string)registryKey.GetValue("Size");
                }
                finally
                {
                    registryKey.Close();
                }
            }
            return accountConfig;
        }

        public static void Save(string email, string apiKey, string searchKey, string fields, string size)
        {
            Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\put.zone\FofaSearch", true);
            if (registryKey == null)
            {
                registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\put.zone\FofaSearch");
            }
            registryKey.SetValue("Email", email);
            registryKey.SetValue("ApiKey", apiKey);
            registryKey.SetValue("SearchKey", searchKey);
            registryKey.SetValue("Fields", fields);
            registryKey.SetValue("Size", size);
            registryKey.Close();
        }
    }
}